<?php

/**
 * Employee filter form base class.
 *
 * @package    Amawal Invest
 * @subpackage filter
 * @author     Ameen Shoman<amin.shoman@gmail.com>
 */
abstract class BaseEmployeeFormFilter extends BaseFormFilterPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'code'                     => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'email'                    => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'card_id'                  => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'department_id'            => new sfWidgetFormPropelChoice(array('model' => 'CodeDepartment', 'add_empty' => true)),
      'section_id'               => new sfWidgetFormPropelChoice(array('model' => 'CodeSection', 'add_empty' => true)),
      'mobil_no'                 => new sfWidgetFormFilterInput(),
      'hiring_date'              => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate())),
      'birth_date'               => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate())),
      'branch_id'                => new sfWidgetFormPropelChoice(array('model' => 'Branch', 'add_empty' => true)),
      'in_active'                => new sfWidgetFormChoice(array('choices' => array('' => 'yes or no', 1 => 'yes', 0 => 'no'))),
      'link_employee_leave_list' => new sfWidgetFormPropelChoice(array('model' => 'CodeLeaveType', 'add_empty' => true)),
    ));

    $this->setValidators(array(
      'code'                     => new sfValidatorPass(array('required' => false)),
      'email'                    => new sfValidatorPass(array('required' => false)),
      'card_id'                  => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'department_id'            => new sfValidatorPropelChoice(array('required' => false, 'model' => 'CodeDepartment', 'column' => 'id')),
      'section_id'               => new sfValidatorPropelChoice(array('required' => false, 'model' => 'CodeSection', 'column' => 'id')),
      'mobil_no'                 => new sfValidatorPass(array('required' => false)),
      'hiring_date'              => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false)))),
      'birth_date'               => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false)))),
      'branch_id'                => new sfValidatorPropelChoice(array('required' => false, 'model' => 'Branch', 'column' => 'id')),
      'in_active'                => new sfValidatorChoice(array('required' => false, 'choices' => array('', 1, 0))),
      'link_employee_leave_list' => new sfValidatorPropelChoice(array('model' => 'CodeLeaveType', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('employee_filters[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function addLinkEmployeeLeaveListColumnCriteria(Criteria $criteria, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $criteria->addJoin(LinkEmployeeLeavePeer::EMPLOYEE_ID, EmployeePeer::ID);

    $value = array_pop($values);
    $criterion = $criteria->getNewCriterion(LinkEmployeeLeavePeer::LEAVE_TYPE_ID, $value);

    foreach ($values as $value)
    {
      $criterion->addOr($criteria->getNewCriterion(LinkEmployeeLeavePeer::LEAVE_TYPE_ID, $value));
    }

    $criteria->add($criterion);
  }

  public function getModelName()
  {
    return 'Employee';
  }

  public function getFields()
  {
    return array(
      'id'                       => 'Number',
      'code'                     => 'Text',
      'email'                    => 'Text',
      'card_id'                  => 'Number',
      'department_id'            => 'ForeignKey',
      'section_id'               => 'ForeignKey',
      'mobil_no'                 => 'Text',
      'hiring_date'              => 'Date',
      'birth_date'               => 'Date',
      'branch_id'                => 'ForeignKey',
      'in_active'                => 'Boolean',
      'link_employee_leave_list' => 'ManyKey',
    );
  }
}
